broadcast静态使用
在AndroidManifest.xml中注册
1 2 3 4 5 6
| <receiver android:name="com.example.testBR"> <intent-filter> <action android:name="com.example.test.ACTION_TEST" /> </intent-filter> </receiver>
|
重写onReceive方法
1 2 3 4
| @Override public void onReceive(Context context, Intent intent) { }
|
broadcast动态注册
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private BroadcastReceiver mReceiver = new BroadcastReceiver(){
@Override public void onReceive(Context context, Intent intent) { } };
@Override protected void onPause() { super.onPause(); unregisterReceiver(mReceiver); }
@Override protected void onResume() { super.onResume(); IntentFilter filter = new IntentFilter("com.example.test.ACTION_TEST"); registerReceiver(mReceiver, filter);
}
|
发送广播
1
| sendBroadcast(new Intent("com.example.test.ACTION_TEST"));
|
Intent中附加信息
发送带有附加消息的Intent
1 2 3
| Intent i = new Intent(this, TestService.class); i.putExtra(name, value); sendBroadcast(new Intent("com.example.test.ACTION_TEST"));
|
接收Intent中的附加消息
1 2 3 4
| @Override public void onReceive(Context context, Intent intent) { intent.getIntExtra("action", 0)); }
|
broadcast权限
如果reeceiver声明在AndroidManifest.xml
中,且仅限应用内部使用,则可在标签上添加android:exported="false"
属性
这样系统的其他应用就再也无法接触到该receiver。
另外也可以通过自己创建使用权限,在AndroidManifest.xml
中添加标签来完成。
1 2 3 4 5 6
| <permission android:name="com.example.test.TestService.PRIVATE" android:protectionLevel="signature" ></permission>
<uses-permission android:name="com.example.test.TestService.PRIVATE"/>
|
发送带权限的broadcast
1 2
| String PREM_PRIVATE = "com.example.test.TestService.PRIVATE"; sendBroadcast(new Intent("com.example.test.ACTION_TEST"),PREM_PRIVATE);
|
通过ADB shell模拟发送broadcast
1 2 3 4 5 6 7 8 9 10 11 12
| adb shell am broadcast <option> <INTENT>:
option: [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>] [-c <CATEGORY> [-c <CATEGORY>] ...] [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...] [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...] [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...] [-n <COMPONENT>] [-f <FLAGS>] [<URI>]
|
例如:
1
| adb shell am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true
|